<script src="http://maps.google.cn/maps/api/js?sensor=false"></script>

<script>
var map;
var myCenter=new google.maps.LatLng(51.508742,-0.120850);
var x=new google.maps.LatLng(52.395715,4.888916);
var stavanger=new google.maps.LatLng(58.983991,5.734863);
var amsterdam=new google.maps.LatLng(52.395715,4.888916);
var london=new google.maps.LatLng(51.508742,-0.120850);

var addressList = [ 
<{foreach from=$days item=day}>
    "<{$day.address}>",
<{/foreach}>
];

var addressDayList = <{$address}>;
var locations = [];
var dayLocations = [];
//console.log(addressDayList);
var bounds = new google.maps.LatLngBounds();
var boundsCounts = 0;

function initLocation ()
{
    var geocoder = new google.maps.Geocoder();

    for (idx in addressList)
    {
        addr = addressList[idx];
        geocoder.geocode( {  
            'address' : addr
        }, function(results, status) {  
            if (status == google.maps.GeocoderStatus.OK) {  

                //results数组里有很多有用的信息，包括坐标和返回的标准位置信息  
                myLatLng = results[0].geometry.location;  

                bounds.extend(myLatLng);
                locations.push([results[0].formatted_address, myLatLng]);

                if (boundsCounts == 0)
                    myCenter = myLatLng;

                boundsCounts += 1;
                if (boundsCounts == addressList.length)
                {
                    google.maps.event.addDomListener(window, 'load', initialize);
                }

            } else {  
                alert(interGeoAnalysisFailed);  
            }  
        });  
    }


}

function initialize()
{
    var mapProp = {
        center:myCenter,
        zoom:4,
        disableDefaultUI:false,
        mapTypeId:google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
    map.fitBounds(bounds);

    var myTrip=[];

    for (idx in locations)
    {
        // 地图坐标锚点
        var addr = locations[idx][0];
        var loc  = locations[idx][1];
        myTrip.push(loc);
        var marker=new google.maps.Marker({
            position: loc
            // animation:google.maps.Animation.BOUNCE
        });

        marker.setMap(map);

        /*
        // 信息提示窗口
        var infowindow = new google.maps.InfoWindow({
            content: addr
        });

        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker);
        });
        */
    }


    // 折线
    var flightPath=new google.maps.Polyline({
        path:myTrip,
        strokeColor:"#8800FF",
        strokeOpacity:0.5,
        strokeWeight:3
    });

    flightPath.setMap(map);
    reinitialize(1);

}
function reinitialize(day)
{
    if(day != 999){

        var mapProp = {
            center:myCenter,
            zoom:4,
            disableDefaultUI:false,
            mapTypeId:google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
        map.fitBounds(bounds);
        var key = 'day'+day;
        var arr = [];
//        console.log(addressDayList);

        for(loc in addressDayList){
            if(key == loc){
                arr = addressDayList[loc];
            }
        }
        console.log(arr);

        var myTrip=[];
        var post = [];
        var id = 0;
        var geocoder = new google.maps.Geocoder();

        for (idx in arr)
        {
            var addr = arr[idx];
//            console.log(addr.lat);
//            console.log(addr.lng);
//            console.log(addr.address);
//            console.log(idx);

            if(addr.lat == null || addr.lng == null){

                geocoder.geocode( {
                    'address' : addr.address
                }, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {

                        //results数组里有很多有用的信息，包括坐标和返回的标准位置信息
                        myLatLng = results[0].geometry.location;
                        myTrip.push(myLatLng);
                        post.push(results);
                        id++;
                        var marker=new google.maps.Marker({
                            position: myLatLng
                            // animation:google.maps.Animation.BOUNCE
                        });

                        marker.setMap(map);
                    }
                });

            }else{
                myLatLng = new google.maps.LatLng(addr.lat,addr.lng);
                myTrip.push(myLatLng);
                var marker=new google.maps.Marker({
                    position: myLatLng
                    // animation:google.maps.Animation.BOUNCE
                });

                marker.setMap(map);
            }



        }


        setTimeout(function(){
//            console.log(myTrip);
//            console.log(id);
//            console.log(post);
            var flightPath=new google.maps.Polyline({
                path:myTrip,
                strokeColor:"#8800FF",
                strokeOpacity:0.5,
                strokeWeight:3
            });
            flightPath.setMap(map);
            for (idx in post) {
                var url = "/scenicspot/ajax_setlocation/"+arr[idx].id;
//                console.log(url);
//                console.log(post[idx][0]);
                lng = post[idx][0].geometry.viewport.N.N;
//                console.log(lng);
                data = {'lng':lng,'lat':post[idx][0].geometry.viewport.j.j}
//                console.log(data);
//                $.post(url,data,function(status,data2){
//
//                });
            }
        },500);


    }else{
        var mapProp = {
            center:myCenter,
            zoom:4,
            disableDefaultUI:false,
            mapTypeId:google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
        map.fitBounds(bounds);

        var myTrip=[];

        for (idx in locations)
        {
            // 地图坐标锚点
            var addr = locations[idx][0];
            var loc  = locations[idx][1];
            myTrip.push(loc);
            var marker=new google.maps.Marker({
                position: loc
                // animation:google.maps.Animation.BOUNCE
            });

            marker.setMap(map);

            /*
             // 信息提示窗口
             var infowindow = new google.maps.InfoWindow({
             content: addr
             });

             google.maps.event.addListener(marker, 'click', function() {
             infowindow.open(map,marker);
             });
             */
        }


        // 折线
        var flightPath=new google.maps.Polyline({
            path:myTrip,
            strokeColor:"#8800FF",
            strokeOpacity:0.5,
            strokeWeight:3
        });

        flightPath.setMap(map);
    }


}

function placeMarker(location) {
  var marker = new google.maps.Marker({
    position: location,
    map: map,
  });
  var infowindow = new google.maps.InfoWindow({
    content: 'Latitude: ' + location.lat() +
    '<br>Longitude: ' + location.lng()
  });
  infowindow.open(map,marker);
}

//google.maps.event.addDomListener(window, 'load', initialize);
initLocation();

reinitialize(1);
function trivial_day(day){
//    alert(day);
    reinitialize(day);



}

</script>

<div id="googleMap" style="width:100%;height:380px;"></div>
